﻿using System;
using System.Linq;
using Loki.Model;
using NUnit.Framework;

namespace Loki.Core.Model.Test
{
    [TestFixture]
    public class UniqueKeyMapperTest : AssertionHelper
    {
        private UniqueKeyMapper<DummyEntity, Guid> _Mapper;
        private UniqueKeyMapper<DummyEntity, string> _StringMapper;

        private DummyEntity _Entity;
        private DummyEntity _Entity2;
        private DummyEntity _Entity3;
        private DummyEntity _Entity4;

        [SetUp]
        public void SetUp()
        {
            _Mapper = new UniqueKeyMapper<DummyEntity, Guid>(x => x.ID, null);
            _StringMapper = new UniqueKeyMapper<DummyEntity, string>(x => x.Key2, StringComparer.InvariantCultureIgnoreCase);

            _Entity = new DummyEntity() { ID = Guid.NewGuid(), Key1 = 1, Key2 = "a", Value = "gnagna" };
            _Entity2 = new DummyEntity() { ID = Guid.NewGuid(), Key1 = 1, Key2 = "b", Value = "gnagna" };
            _Entity3 = new DummyEntity() { ID = Guid.NewGuid(), Key1 = 1, Key2 = "d", Value = "gnagna" };
            _Entity4 = new DummyEntity() { ID = _Entity2.ID, Key1 = 1, Value = "gnagna", Key2 = "d" };

            _Mapper.Register(_Entity);
            _Mapper.Register(_Entity2);

            _StringMapper.Register(_Entity2);
            _StringMapper.Register(_Entity);
        }

        [Test]
        public void TestRegisterAdd()
        {
            Expect(_Mapper.Register(_Entity3),
                Is.True
                );
        }

        [Test]
        public void TestRegisterReplace()
        {
            Expect(_Mapper.Register(_Entity4),
                Is.False
                );
        }

        [Test]
        public void TestGet()
        {
            Expect(_Mapper.Get(_Entity.ID),
                EqualTo(_Entity)
                );
        }

        [Test]
        public void TestGetWithComparer()
        {
            Expect(_StringMapper.Get("B"),
                EqualTo(_Entity2)
                );
        }

        [Test]
        public void TestIsLoadedTrue()
        {
            Expect(_Mapper.IsLoaded(_Entity.ID),
                Is.True
                );
        }

        [Test]
        public void TestIsLoadedFalse()
        {
            Expect(_Mapper.IsLoaded(Guid.NewGuid()),
                Is.False
                );
        }

        [Test]
        public void TestInvalidate()
        {
            //_Mapper.
            Expect(_Mapper.IsLoaded(_Entity.ID),
                Is.False
                );
        }


        [Test]
        public void TestIsLoadedWithComparer()
        {
            Expect(_StringMapper.IsLoaded("B"),
                Is.True
                );
        }

        [Test]
        public void TestGetAllBy()
        {
            Expect(_Mapper.GetAllByKey(_Entity.ID).First(),
                EqualTo(_Entity)
                );
        }

        [Test]
        public void TestUnregister()
        {
            _Mapper.Remove(_Entity);
            Expect(_Mapper.Get(_Entity.ID),
                 Is.Null
                 );
        }
    }
}
